/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: AppLifePresenter
 * Author: Clark
 * Date: 2020/10/7 18:56
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.gover.FunctionItem;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.GetOrderOpenid;
import sz.szsmk.citizencard.model.network.bean.request.ServiceNetReq;
import sz.szsmk.citizencard.model.network.bean.response.GetOrderOpenidRes;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.mvp.contract.IAppLifeContact;
import sz.szsmk.citizencard.mvp.contract.ISerNetworkContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * @ClassName: AppLifePresenter
 * @Description:
 * @Author: Clark
 * @Date: 2020/10/7 18:56
 */
public class AppLifePresenter extends IBasePresenter implements IAppLifeContact.IAppLifePresenter<IAppLifeContact.IAppLifeView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public AppLifePresenter(GsdxNetApi gsdxNetApi){
        this.gsdxNetApi=gsdxNetApi;
        this.mNetworkDateSource=NetworkDateSources.getInstance();
    }

    @Override
    public void getWebsiteList(String service_type, String site_type, String distance_type, int type, String mLatitude, String mLongitude, int pager) {
        ServiceNetReq serviceNetReq = new ServiceNetReq();
        serviceNetReq.setLatitude(mLatitude);
        serviceNetReq.setLongitude(mLongitude);
        serviceNetReq.setDistance_type(distance_type);
        serviceNetReq.setPage_num(pager + "");
        serviceNetReq.setPage_size("15");
        serviceNetReq.setService_type(service_type);
        serviceNetReq.setSite_type(site_type);
        String json = new Gson().toJson(serviceNetReq);
        L.e(Constant.TAG, "getWebsiteList----" + json);
        mNetworkDateSource.websiteList(serviceNetReq, new SubscribeResult<PrepayResponse>() {
            @Override
            protected void onOk(PrepayResponse response) {

                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "getWebsiteList----res" + toJson);
                List<PrepayResponse.WebsiteListInfoBean> websiteListInfo = response.getWebsiteListInfo();
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListSucc(websiteListInfo);
            }

            @Override
            protected void onFailure(PrepayResponse response) {
                super.onFailure(response);
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListFail();

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISerNetworkContact.ISerNetworkView) mView).getWebsiteListFail();

            }
        });
    }

    @Override
    public void getGetOrderId(String url, String type, FunctionItem data) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("HUIMIN");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "LifePresentergetOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                Constant.EXTENT = response.getExtend();
                L.e(Constant.TAG, "LifePresentergetOrderOpenid----->" + response.getExtend());
                ((IAppLifeContact.IAppLifeView) mView).getGetOrderIdSucc(response, type, url, data);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {
                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });

    }

}